import { _decorator } from 'cc';
const BOARD_SIZE = 15;
export enum PieceType {
    None = 0,
    Black = 1,
    White = 2
}

export class Gomoku_Board {
    grid: PieceType[][] = [];
    constructor() {
        this.reset();
    }
    reset() {
        this.grid = [];
        for (let i = 0; i < BOARD_SIZE; i++) {
            this.grid[i] = new Array(BOARD_SIZE).fill(PieceType.None);
        }
    }
    isValid(x: number, y: number): boolean {
        return x >= 0 && x < BOARD_SIZE && y >= 0 && y < BOARD_SIZE && this.grid[x][y] === PieceType.None;
    }
    placePiece(x: number, y: number, type: PieceType): boolean {
        if (this.isValid(x, y)) {
            this.grid[x][y] = type;
            return true;
        }
        return false;
    }
    checkWin(x: number, y: number, type: PieceType): boolean {
        const dirs = [[1, 0], [0, 1], [1, 1], [1, -1]];
        for (let [dx, dy] of dirs) {
            let count = 1;
            for (let d = -1; d <= 1; d += 2) {
                let nx = x + dx * d, ny = y + dy * d;
                while (
                    nx >= 0 && ny >= 0 && nx < BOARD_SIZE && ny < BOARD_SIZE &&
                    this.grid[nx][ny] === type
                ) {
                    count++;
                    nx += dx * d;
                    ny += dy * d;
                }
            }
            if (count >= 5) return true;
        }
        return false;
    }
}